package com.gzgy.ustore.api.inteceptors;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.exception.ExceptionCodeStatusEnum;
import com.gzgy.ustore.api.service.BaseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gzgy.ustore.api.common.util.I18nUtil;

public class Inteceptor implements HandlerInterceptor {

	private Log logger = LogFactory.getLog("REQUEST_PARAMETER_LOG");
	
	@Autowired
	private BaseService baseService;
	//@SuppressWarnings("rawtypes")
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//打印log
		InteceptorUtil.printParamLog(logger,request);

		String userId="";
		String session="";
		userId = request.getParameter("userId");
		session = request.getParameter("session");
		
		Assert.hasText(session, "session "+I18nUtil.getMessage(-2, null, null));
		Assert.hasText(userId, "userId "+I18nUtil.getMessage(-2, null, null));
		
		logger.info("userId["+userId+"] ,session["+session+"]");
		
		if(!baseService.loginCheck(userId,session)){
			Integer errorCode = ExceptionCodeStatusEnum.SESSION_EXCEPTION.toInt();
			String errorMsg = I18nUtil.getMessage(errorCode,null,"zh_CN");
			throw new BaseAPIException(errorMsg,errorCode);
		}
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
